<template>
  <div class="app-container calendar-list-container">
    <div v-if="!opShow">
      <search :list="queryList" :queryEntity="queryEntity"></search>
      <buttons :showQuery="true" @query="doQuery" :list="btnList" :disabled="disabled" placeholder="公司名称" :permissions="permissions" @add="doAdd" @edit="doEdit" @view="doView"
        @delete="doDelete" @user="openUser" @open="doOpen" @close="doClose"></buttons>
      <grid :columns="columns" :multi="true" :url="queryUrl" ref="mainGrid" height="auto" diffHeight="105" @change="rowCilck" :pagination="true" :queryEntity="queryEntity"></grid>
    </div>
    <op ref="op" :visible="opShow" @cancel="cancel"></op>
  </div>
</template>

<script>
import request from '@/router/axios'
import url from '@/const/apiURL'
import grid from '@/components/Grid'
import buttons from '@/components/Buttons'
import search from '@/components/Search'
import op from './op'
import { Message, MessageBox } from 'element-ui'
import { initMenu } from "@/util/util";
import { resolveUrlPath } from "@/util/util";
import { mapGetters } from 'vuex'
import { YesOrNoFormatter } from '@/const/dict'
import { dict } from '@/const/dict'

export default {
  components: { grid, buttons, op, search },
  data () {
    return {
      queryUrl: url.COMPANY_MANAGER_LIST_URL,
      disabled: true,
      opShow: false,
      queryEntity: {},
      columns: [
        { label: '公司名称', width: 200, prop: 'companyName', align: 'left' },
        { label: '统一社会信用代码', width: 150, prop: 'creditCode', align: 'left' },
        { label: '手机号码', width: 120, prop: 'mobile', align: 'left' },
        { label: '是否启用', width: 80, prop: 'status', align: 'center', formatter: YesOrNoFormatter },
        { label: '排序', width: 80, prop: 'indexSort', align: 'center', sortable: 'custom', order: 'index_sort' },
        { label: '创建人', width: 120, prop: 'createUserName', align: 'center' },
        { label: '创建时间', width: 100, prop: 'createTime', align: 'center' }
      ],
      btnList: [
        { name: '新增', type: 'primary', icon: 'edit', click: 'add', auth: 'manager_company_add', ctr: false },
        { name: '编辑', type: 'primary', icon: 'edit', click: 'edit', auth: 'manager_company_edit', ctr: true },
        { name: '查看', type: 'primary', icon: 'edit', click: 'view', auth: 'manager_company_view', ctr: true },
        { name: '开启', type: 'primary', icon: 'edit', click: 'open', auth: 'manager_company_open', ctr: true, confirm: true },
        { name: '关闭', type: 'primary', icon: 'edit', click: 'close', auth: 'manager_company_close', ctr: true, confirm: true },
        { name: '用户', type: 'primary', icon: 'edit', click: 'user', auth: 'manager_company_user', ctr: true }
      ],
      queryList: [
        { name: '公司名称', type: 'text', prop: 'companyNameLike', placeholder: "公司名称" },
        { name: '是否启用', type: 'select', prop: 'status', placeholder: "是否启用", values: dict().YesOrNo, label: 'name', value: 'value' }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'permissions'
    ])
  },
  created () {

  },
  methods: {
    doAdd () {
      this.opShow = true
      this.$refs['op'].init('新增', {}, false, url.COMPANY_MANAGER_ADD_URL)
    },
    doEdit () {
      const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.viewOp(row.companyCode).then((data) => {
          this.opShow = true
          this.$refs['op'].init('编辑', data, false, url.COMPANY_EDIT_URL)
        })
      }

    },
    doView () {
      const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.viewOp(row.companyCode).then((data) => {
          this.opShow = true
          this.$refs['op'].init('查看', data, true)
        })
      }
    },
    doDelete () {
      const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        MessageBox.confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 删除数据
          this.deleteOp(row.companyCode).then((data) => {
            if (data) {
              this.$refs['mainGrid'].reload()
            }
          })
        })
      }
    },
    doQuery (text) {
      this.$refs['mainGrid'].doQuery(text)
    },
    rowCilck (row) {
      this.disabled = row.length == 0
    },
    cancel () {
      this.opShow = false
    },
    deleteOp (code) {
      return request({
        url: url.COMPANY_DELETE_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { 'companyCode': code }
      })
    },
    viewOp (companyCode) {
      return request({
        url: url.COMPANY_VIEW_URL,
        method: 'post',
        params: { companyCode },
        data: { companyCode }
      })
    },
    openOp (code) {
      return request({
        url: url.COMPANY_OPEN_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { 'companyCode': code }
      })
    },
    closeOp (code) {
      return request({
        url: url.COMPANY_CLOSE_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { 'companyCode': code }
      })
    },
    doOpen () {
      const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.openOp(row.companyCode).then((data) => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    doClose () {
      const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.closeOp(row.companyCode).then((data) => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    openUser () {
      const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.$router.push({ path: '/admin/company/user', query: { companyCode: row.companyCode } })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
